package com.db.person.controller;

import com.db.person.bean.User_inf;
import com.db.person.service.IUserService;
import com.db.person.util.SendEmailUtil;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RepasswordController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/password/repassword")
    public String toRepassword(){
        return "repasswordPage";
    }

    @RequestMapping("/password/findPassword")
    public String sendMail(String loginname, String username, String email, Model model){

        //1.判断 登录名 用户名 邮箱 必须匹配上一条数据  select * from user_inf where loginname=.. and username=.. and email=...
        User_inf user = userService.getByEmail(loginname, username, email);
        if(user!=null){
            //2.编写发送邮件的代码
            String content = "请点击<a href='http://127.0.0.1:8082/password/toFind?abc="+loginname+"'>找回密码</a>";
            SendEmailUtil.sendEmail("546881916@qq.com",email,"找回密码",content);

            model.addAttribute("message","邮件已发送");
        }else{
            //3.用户名 登录名 邮箱 不匹配
            model.addAttribute("message","信息不匹配");
        }


        return "repasswordPage";
    }

    @RequestMapping("/password/toFind")
    public String toFind(String abc,Model model){
        model.addAttribute("loginname",abc);
        return "findPwd";
    }

    @RequestMapping("/password/setPassword")
    public String setPwd(String loginname, String password){

        SimpleHash simpleHash = new SimpleHash("MD5",password,loginname,1024);
        String realPwd = simpleHash.toString();

        userService.setPwd(loginname,realPwd);

        return "loginForm";
    }

}
